<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrdersController extends Controller
{

    /**
     * 订单
     */
    public function postIndex(Request $request)
    {
      // $userid = $request->session()->get('id');
      $userid = session('id');
      $data1 = DB::table('useraddress')
      ->where('userid',$userid)
      ->orderBy('id','desc')
      ->limit(2)
      ->get();
      // dd($data1);

      $arr = $request->all();
      $zongjia = $arr['zongjia'];
        //dd($arr);
      $result = []; 
      foreach ($arr['g_id'] as $key => $val ) { 
          $result[$key][] = $val; 
          $result[$key][] = $arr['jiancheng'][$key];
          $result[$key][] = $arr['price'][$key];
          $result[$key][] = $arr['number'][$key];
          $result[$key][] = $arr['goodspic'][$key];
          $result[$key][] = $arr['tname'][$key];

      } 
      // dd($result);
      // foreach($result as $k => $v){var_dump($v);}
      //   dd();


      return view('home.order',['address'=>$data1,'result'=>$result,'total'=>$zongjia]);
      
    }

    /**
     * ajax处理
     */
    public function getAjax(Request $request)
    {
      $id = $request->input('id');
      // $userid = $request->input('userid');
      // 从session中得到用户id
      // $userid = $request->session()->get('id');
      $userid = session('id');
      
      // 将得到的地址id放入session中
      $request->session()->put('address_id',$id);


      $date = DB::table('useraddress')
      ->where('userid',$userid)
      ->update(['status'=>0]);

      $date1 = DB::table('useraddress')
        ->where('id',$id)
        ->update(['status'=>1]);

      if($date > 0 && $date1 > 0){
        echo 1;
      }else{
        echo 0;
      }  
    }

    /**
     * 插入数据
     */
    public function postInsert(Request $request)
    {
      // $userid = $request->session()->get('id');
      // $userid = 1;
      $userid = session('id');
      // $user = 'adminss';
      $user = DB::table('users')
      ->where('id',$userid)
      ->get();
      foreach($user as $k=>$v){
        $abc = $v->username;
      }

      $sj = $request->only('getman','phone','address','sheng','shi','xian','mailcode');
      $sj['userid'] = $userid;
      $sj['name'] = $abc;
      $date = DB::table('useraddress')->insert($sj);
      if($date > 0){
        //成功 重定向 页面跳转到列表页
        return redirect('/');
      }else{
        //失败 重定向 回到当前页
        return back()->with('error','地址添加失败');
      }
    }


    /**
     * 创建订单
     */
    public function postCreate(Request $request)
    {
      // echo "string";
      // 获取传过来的数据
      $data = $request->only('goods_id','goods_name','price','num','tname','total');

      // $data['user_id'] = $userid;

      // $data['address_id'] = $address_id;
      // // 调用获取订单编号的方法得到编号
      // $data['order_code'] = $this->getOrderCode();
      // $data['order_time'] = date('Y-m-d H:i:s',time());
      // dd($data);

      // 插入orders表中
      $order_code = $this->getOrderCode();
      $total = $request->input('total');
      // $userid = $request->session()->get('id');
      $userid = session('id');
      
      $address_id = $request->session()->get('address_id');
      $order_time = date('Y-m-d H:i:s',time());

      $order = ['order_code'=>$order_code,'user_id'=>$userid,'total'=>$total,'order_time'=>$order_time,'address_id'=>$address_id];

      // 插入数据到表中,并获取其id
      $order_id = DB::table('orders')->insertGetId($order);
      // dd($order_id);

      // 获取order_goods表要用的数据
      $goods_id = $request->input('goods_id');
      $goods_name = $request->input('goods_name');
      $num = $request->input('num');
      $tname = $request->input('tname');
      $order_goods = ['order_id'=>$order_id,'goods_id'=>$goods_id,'goods_name'=>$goods_name,'num'=>$num,'tname'=>$tname];
      // 插入order_goods表中
      $dingdan = DB::table('order_goods')->insert($order_goods);
      // dd($dingdan);
      if($dingdan){
        //成功 重定向 页面跳转到列表页
        return redirect('/')->with('success','1');
      }else{
        //失败 重定向 回到当前页
        return back()->with('error','0');
      }
      
    }




    /**
     * 获取订单标号
     */
    public function getOrderCode()
    {
      return time().rand(100000,9999999);
    }
}
